/*****************************************************************************
**  Project       
**  (c) copyright 2023

**  Company       
**                All rights reserved
**  Secrecy Level STRICTLY CONFIDENTIAL
*******************************************************************************
**
**          File  : CAP_InteractionAppHandlerProcess.c
**          Description: 与其他模块进行协议交互的处理文件
**          Author: mcu_tools
**
**          Date  : 2023-07-04 16:37:54
**
******************************************************************************/

#ifndef _CAP_INTERACTIONAPPHANDLERPROCESS_C_
#define _CAP_INTERACTIONAPPHANDLERPROCESS_C_

#include "CAP_InteractionAppHandlerProcess.h"
#include "CAP_InteractionAppHandlerProcess.h"
#include "CAP_InteractionAppHandlerIfal.h"
#include "Rte_Temp.h"
#include "VDTH_UtilsLog.h"

#define ANDROID_FDBUS_INTERACTION_CMD      90

// channel index and id: 1
// channel name: com.bosch.cm.inc.vehicle_set

static uint8 u8ChnSendBuffer[255] = {0};
static int s_DataLen = 0;

/*  receive data, variable definition */
// 无感登录数据
InsensitiveLogin_t s_InsensitiveLogin_Data = {0};
// 氛围灯数据
AtmosphereLamp_t s_AtmosphereLamp_Data = {0};
// HUD功能数据
CarSettingHud_t s_CarSettingHud_Data = {0};
// DMS模块
VimsDms_t s_VimsDms_Data = {0};
// OMS模块
VimsOms_t s_VimsOms_Data = {0};
// 车辆设置补充
DoorSeatModule_t s_DoorSeatModule_Data = {0};
// ADAS
AdasSetting_t s_AdasSetting_Data = {0};
// 座椅记忆
SeatMirrorBind_t s_SeatMirrorBind_Data = {0};
// 新能源相关
New_Energy_t s_New_Energy_Data = {0};
// RVC&AVM
RVCAVM_TYPE_t s_RVCAVM_TYPE_Data = {0};
// RVC&AVM
HVAC_TYPE_t s_HVAC_TYPE_Data = {0};
// RVC&AVM
UserIdOperate_t s_UserIdOperate_Data = {0};
// 
Hut_Settings_t s_Hut_Settings_Data = {0};
// 副驾座椅记忆功能
PassSeatBind_t s_PassSeatBind_Data = {0};
// C03_C01记忆（上电变化下发）
C03_C01_TYPE_t s_C03_C01_TYPE_Data = {0};
// 欧盟记忆
EU_TYPE_t s_EU_TYPE_Data = {0};
// 用户登录状态（上电下发）
APP_LOGIN_TYPE_t s_APP_LOGIN_TYPE_Data = {0};
// 通风加热等同帧数据
APP_Wind_t s_APP_Wind_Data = {0};
// 获取的所有数据
GetAllData_t s_GetAllData_Data = {0};
// 获取单组数据
GetOneData_t s_GetOneData_Data = {0};
/*  receive groups total:20 */

static uint8 s_FdBus_90_getAlldataFlg = 0;
static boolean s_bChnReady = FALSE;
static boolean s_bReadIng = FALSE;
static boolean s_bIsFirst = TRUE;


// E_IAPP_MDL_Max (group num): 20
static boolean s_bIsFirstS[E_IAPP_MDL_Max] = {
    FALSE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    FALSE,
    FALSE,
    
}; 

void vipIAppFlowCtrlCB(uint8 channelNumber, uint8 channelThrottleStatus);
void vipIAppRxMsgCB(uint8 channelNumber, uint8* buf, int* lenptr);


void IAPP_ProcessInit(void) {
    (void)IAPPChnOpenClbkReg(vipIAppFlowCtrlCB, vipIAppRxMsgCB);
    s_GetAllData.mdl_id = E_IAPP_GetAll;
    s_GetOneData.mdl_id = E_IAPP_GetSimple;
    s_InsensitiveLogin_Data.mdl_id = E_IAPP_InsensitiveLogin;
    s_InsensitiveLogin_Data.result = INTERACTION_RESULT_FAILED;
    AtmosphereLamp_Data.mdl_id = E_IAPP_CarSettingsFormA;
    AtmosphereLamp_Data.result = INTERACTION_RESULT_SUCCES;
    CarSettingHud_Data.mdl_id = E_IAPP_CarSettingsHUD;
    CarSettingHud_Data.result = INTERACTION_RESULT_SUCCES;
    VimsDms_Data.mdl_id = E_IAPP_VimsDms;
    VimsDms_Data.result = INTERACTION_RESULT_SUCCES;
    VimsOms_Data.mdl_id = E_IAPP_VimsOms;
    VimsOms_Data.result = INTERACTION_RESULT_SUCCES;
    DoorSeatModule_Data.mdl_id = E_IAPP_DoorSeatModule;
    DoorSeatModule_Data.result = INTERACTION_RESULT_SUCCES;
    AdasSetting_Data.mdl_id = E_IAPP_Adas;
    AdasSetting_Data.result = INTERACTION_RESULT_SUCCES;
    SeatMirrorBind_Data.mdl_id = E_IAPP_SeatMirrorBind;
    SeatMirrorBind_Data.result = INTERACTION_RESULT_SUCCES;
    New_Energy_Data.mdl_id = E_IAPP_new_energy;
    New_Energy_Data.result = INTERACTION_RESULT_SUCCES;
    RVCAVM_TYPE_Data.mdl_id = E_IAPP_APP_RVCAVM_TYPE;
    RVCAVM_TYPE_Data.result = INTERACTION_RESULT_SUCCES;
    HVAC_TYPE_Data.mdl_id = E_IAPP_HVAC_TYPE;
    HVAC_TYPE_Data.result = INTERACTION_RESULT_SUCCES;
    UserIdOperate_Data.mdl_id = E_IAPP_APP_RVCAVM_TYPE;
    UserIdOperate_Data.result = INTERACTION_RESULT_SUCCES;
    Hut_Settings_Data.mdl_id = E_IAPP_HUT_Settings;
    Hut_Settings_Data.result = INTERACTION_RESULT_SUCCES;
    PassSeatBind_Data.mdl_id = E_IAPP_PASSENGER_SEAT;
    PassSeatBind_Data.result = INTERACTION_RESULT_SUCCES;
    C03_C01_TYPE_Data.mdl_id = E_IAPP_C03_C01_TYPE;
    C03_C01_TYPE_Data.result = INTERACTION_RESULT_SUCCES;
    EU_TYPE_Data.mdl_id = E_IAPP_EU_TYPE;
    EU_TYPE_Data.result = INTERACTION_RESULT_SUCCES;
    APP_LOGIN_TYPE_Data.mdl_id = E_IAPP_APP_LOGIN_TYPE;
    APP_LOGIN_TYPE_Data.result = INTERACTION_RESULT_SUCCES;
    APP_Wind_Data.mdl_id = E_IAPP_APP_WIND;
    APP_Wind_Data.result = INTERACTION_RESULT_SUCCES;
    GetAllData_Data.mdl_id = E_IAPP_GetAll;
    GetAllData_Data.result = INTERACTION_RESULT_SUCCES;
    GetOneData_Data.mdl_id = E_IAPP_GetSimple;
    GetOneData_Data.result = INTERACTION_RESULT_SUCCES;
}

void IAPP_ProcessLoop(void) {
#if 0
    if (FALSE == s_bChnReady) {
        // reconnect
    }
#endif

    if (FALSE == s_bChnReady) {
        if (IAPPChnIsChanOpen(CHN_InteractionAPP)) {
            s_bChnReady = TRUE;
        }
    }
}

void vipIAppSendData(interaction_mdl_id_t id, uint8 *buf, uint16 size) {
    s_DataLen = size + 1;
    u8ChnSendBuffer[0] = id;
    memcpy(&u8ChnSendBuffer[1], buf, size);
    
    if (FALSE == s_bIsFirst) {
        V_DltPrintf(LOG_INFO, E_MDL_SETTING_10MS, "vipIAppSend id:%d s:%d", id, size);
    }

    if ((s_bReadIng == FALSE) &&
            (s_bIsFirst == TRUE)) {
        switch (id) {
            case E_IAPP_InsensitiveLogin:
                s_bIsFirstS[E_IAPP_InsensitiveLogin] = FALSE;
                // memcpy(&s_InsensitiveLogin_Data, buf, size);  // no care
                break;
            
            case AtmosphereLamp:
                s_bIsFirstS[E_IAPP_CarSettingsFormA] = FALSE;
                memcpy(&s_AtmosphereLamp_Data, buf, size);
                break;
                
            case CarSettingHud:
                s_bIsFirstS[E_IAPP_CarSettingsHUD] = FALSE;
                memcpy(&s_CarSettingHud_Data, buf, size);
                break;
                
            case VimsDms:
                s_bIsFirstS[E_IAPP_VimsDms] = FALSE;
                memcpy(&s_VimsDms_Data, buf, size);
                break;
                
            case VimsOms:
                s_bIsFirstS[E_IAPP_VimsOms] = FALSE;
                memcpy(&s_VimsOms_Data, buf, size);
                break;
                
            case DoorSeatModule:
                s_bIsFirstS[E_IAPP_DoorSeatModule] = FALSE;
                memcpy(&s_DoorSeatModule_Data, buf, size);
                break;
                
            case AdasSetting:
                s_bIsFirstS[E_IAPP_Adas] = FALSE;
                memcpy(&s_AdasSetting_Data, buf, size);
                break;
                
            case SeatMirrorBind:
                s_bIsFirstS[E_IAPP_SeatMirrorBind] = FALSE;
                memcpy(&s_SeatMirrorBind_Data, buf, size);
                break;
                
            case New_Energy:
                s_bIsFirstS[E_IAPP_new_energy] = FALSE;
                memcpy(&s_New_Energy_Data, buf, size);
                break;
                
            case RVCAVM_TYPE:
                s_bIsFirstS[E_IAPP_APP_RVCAVM_TYPE] = FALSE;
                memcpy(&s_RVCAVM_TYPE_Data, buf, size);
                break;
                
            case HVAC_TYPE:
                s_bIsFirstS[E_IAPP_HVAC_TYPE] = FALSE;
                memcpy(&s_HVAC_TYPE_Data, buf, size);
                break;
                
            case UserIdOperate:
                s_bIsFirstS[E_IAPP_APP_RVCAVM_TYPE] = FALSE;
                memcpy(&s_UserIdOperate_Data, buf, size);
                break;
                
            case Hut_Settings:
                s_bIsFirstS[E_IAPP_HUT_Settings] = FALSE;
                memcpy(&s_Hut_Settings_Data, buf, size);
                break;
                
            case PassSeatBind:
                s_bIsFirstS[E_IAPP_PASSENGER_SEAT] = FALSE;
                memcpy(&s_PassSeatBind_Data, buf, size);
                break;
                
            case C03_C01_TYPE:
                s_bIsFirstS[E_IAPP_C03_C01_TYPE] = FALSE;
                memcpy(&s_C03_C01_TYPE_Data, buf, size);
                break;
                
            case EU_TYPE:
                s_bIsFirstS[E_IAPP_EU_TYPE] = FALSE;
                memcpy(&s_EU_TYPE_Data, buf, size);
                break;
                
            case APP_LOGIN_TYPE:
                s_bIsFirstS[E_IAPP_APP_LOGIN_TYPE] = FALSE;
                memcpy(&s_APP_LOGIN_TYPE_Data, buf, size);
                break;
                
            case APP_Wind:
                s_bIsFirstS[E_IAPP_APP_WIND] = FALSE;
                memcpy(&s_APP_Wind_Data, buf, size);
                break;
                
            case GetAllData:
                s_bIsFirstS[E_IAPP_GetAll] = FALSE;
                memcpy(&s_GetAllData_Data, buf, size);
                break;
                
            case GetOneData:
                s_bIsFirstS[E_IAPP_GetSimple] = FALSE;
                memcpy(&s_GetOneData_Data, buf, size);
                break;
                
            default:
                break;
        }
        if (
            (s_bIsFirstS[E_IAPP_CarSettingsFormA] == FALSE) &&
            (s_bIsFirstS[E_IAPP_CarSettingsHUD] == FALSE) &&
            (s_bIsFirstS[E_IAPP_VimsDms] == FALSE) &&
            (s_bIsFirstS[E_IAPP_VimsOms] == FALSE) &&
            (s_bIsFirstS[E_IAPP_DoorSeatModule] == FALSE) &&
            (s_bIsFirstS[E_IAPP_Adas] == FALSE) &&
            (s_bIsFirstS[E_IAPP_SeatMirrorBind] == FALSE) &&
            (s_bIsFirstS[E_IAPP_new_energy] == FALSE) &&
            (s_bIsFirstS[E_IAPP_APP_RVCAVM_TYPE] == FALSE) &&
            (s_bIsFirstS[E_IAPP_HVAC_TYPE] == FALSE) &&
            (s_bIsFirstS[E_IAPP_APP_RVCAVM_TYPE] == FALSE) &&
            (s_bIsFirstS[E_IAPP_HUT_Settings] == FALSE) &&
            (s_bIsFirstS[E_IAPP_PASSENGER_SEAT] == FALSE) &&
            (s_bIsFirstS[E_IAPP_C03_C01_TYPE] == FALSE) &&
            (s_bIsFirstS[E_IAPP_EU_TYPE] == FALSE) &&
            (s_bIsFirstS[E_IAPP_APP_LOGIN_TYPE] == FALSE) &&
            (s_bIsFirstS[E_IAPP_APP_WIND] == FALSE) &&
            (s_bIsFirstS[E_IAPP_GetAll] == FALSE) &&
            (s_bIsFirstS[E_IAPP_GetSimple] == FALSE)) 
        {
            s_bIsFirst = FALSE;
        }
    }

    if (s_bChnReady) {
        V_DltPrintf(LOG_INFO, E_MDL_SETTING_10MS, "IAPPChnWrite id:%d s:%d", id, size);
        if (0x01 == IAPPChnWrite(u8ChnSendBuffer, &s_DataLen)) {
            // s_bChnReady = FALSE;  // need to reconnect
        } else {
        }
    } else {
        // channel is not ready
    }
}

void vipIAppFlowCtrlCB(uint8 channelNumber, uint8 channelThrottleStatus) {
    // V_DltPrintf(LOG_INFO, E_MDL_SETTING_10MS, "------vipIAppFlowCtrlCB Enter------");

    if (CHN_InteractionAPP == channelNumber) {
        if (0x01 == channelThrottleStatus) {
            s_bChnReady = TRUE;
        } else {
            // do nothing
        }
    } else {
        // do nothing
    }
}

// channelnum
// byte0: channelid 90
// byte1: length
// byte2: length
// byte3: msgid
// byte4: playload
// struct{byte3, byte4......}
void vipIAppRxMsgCB(uint8 channelNumber, uint8* buf, int* lenptr) {
    // V_DltPrintf(LOG_INFO, E_MDL_SETTING_10MS, "------vipIAppRxMsgCB Enter------");
    if (*lenptr > 3) {
        V_DltPrintf(LOG_INFO, E_MDL_SETTING_10MS, "num:%d, len:%d, %d %d %d %d %d %d %d \n", channelNumber, *lenptr, \
                                                    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
        if (CHN_InteractionAPP == channelNumber) {              // chn
            if (buf[0] == ANDROID_FDBUS_INTERACTION_CMD) {      // android msg code
                s_bReadIng = TRUE;
                // buf[3] module handler id
                switch (buf[3]) { 
                    case E_IAPP_NoInductiveLoginFormA:
                        memcpy(&s_InsensitiveLogin_Data, &buf[3], sizeof(InsensitiveLogin_t));
                        break;
                    case E_IAPP_CarSettingsFormA:
                        memcpy(&s_AtmosphereLamp_Data, &buf[3], sizeof(AtmosphereLamp_t));
                        break;
                    case E_IAPP_CarSettingsHUD:
                        memcpy(&s_CarSettingHud_Data, &buf[3], sizeof(CarSettingHud_t));
                        break;
                    case E_IAPP_VimsDms:
                        memcpy(&s_VimsDms_Data, &buf[3], sizeof(VimsDms_t));
                        break;
                    case E_IAPP_VimsOms:
                        memcpy(&s_VimsOms_Data, &buf[3], sizeof(VimsOms_t));
                        break;
                    case E_IAPP_DoorSeatModule:
                        memcpy(&s_DoorSeatModule_Data, &buf[3], sizeof(DoorSeatModule_t));
                        break;
                    case E_IAPP_Adas:
                        memcpy(&s_AdasSetting_Data, &buf[3], sizeof(AdasSetting_t));
                        break;
                    case E_IAPP_SeatMirrorBind:
                        memcpy(&s_SeatMirrorBind_Data, &buf[3], sizeof(SeatMirrorBind_t));
                        break;
                    case E_IAPP_new_energy:
                        memcpy(&s_New_Energy_Data, &buf[3], sizeof(New_Energy_t));
                        break;
                    case E_IAPP_APP_RVCAVM_TYPE:
                        memcpy(&s_RVCAVM_TYPE_Data, &buf[3], sizeof(RVCAVM_TYPE_t));
                        break;
                    case E_IAPP_HVAC_TYPE:
                        memcpy(&s_HVAC_TYPE_Data, &buf[3], sizeof(HVAC_TYPE_t));
                        break;
                    case E_IAPP_APP_RVCAVM_TYPE:
                        memcpy(&s_UserIdOperate_Data, &buf[3], sizeof(UserIdOperate_t));
                        break;
                    case E_IAPP_HUT_Settings:
                        memcpy(&s_Hut_Settings_Data, &buf[3], sizeof(Hut_Settings_t));
                        break;
                    case E_IAPP_PASSENGER_SEAT:
                        memcpy(&s_PassSeatBind_Data, &buf[3], sizeof(PassSeatBind_t));
                        break;
                    case E_IAPP_C03_C01_TYPE:
                        memcpy(&s_C03_C01_TYPE_Data, &buf[3], sizeof(C03_C01_TYPE_t));
                        break;
                    case E_IAPP_EU_TYPE:
                        memcpy(&s_EU_TYPE_Data, &buf[3], sizeof(EU_TYPE_t));
                        break;
                    case E_IAPP_APP_LOGIN_TYPE:
                        memcpy(&s_APP_LOGIN_TYPE_Data, &buf[3], sizeof(APP_LOGIN_TYPE_t));
                        break;
                    case E_IAPP_APP_WIND:
                        memcpy(&s_APP_Wind_Data, &buf[3], sizeof(APP_Wind_t));
                        break;
                    case E_IAPP_GetAll:
                        memcpy(&s_GetAllData_Data, &buf[3], sizeof(GetAllData_t));
                        break;
                    case E_IAPP_GetSimple:
                        memcpy(&s_GetOneData_Data, &buf[3], sizeof(GetOneData_t));
                        break;
                    default:
                        break;
                }
                if(buf[3] = E_IAPP_GetAll)
                {
                    set_flag_90FDBUS_GetAll();
                }
                s_bReadIng = FALSE;
            }
        } else {
            // do nothing
        }
    }
}

void get_vipIAppData(interaction_mdl_id_t id, uint8 *data) {
    switch (id) { 
        case E_IAPP_NoInductiveLoginFormA:
            memcpy(data, &s_InsensitiveLogin_Data, sizeof(InsensitiveLogin_t));
            break;
        case E_IAPP_CarSettingsFormA:
            memcpy(data, &s_AtmosphereLamp_Data, sizeof(AtmosphereLamp_t));
            break;
        case E_IAPP_CarSettingsHUD:
            memcpy(data, &s_CarSettingHud_Data, sizeof(CarSettingHud_t));
            break;
        case E_IAPP_VimsDms:
            memcpy(data, &s_VimsDms_Data, sizeof(VimsDms_t));
            break;
        case E_IAPP_VimsOms:
            memcpy(data, &s_VimsOms_Data, sizeof(VimsOms_t));
            break;
        case E_IAPP_DoorSeatModule:
            memcpy(data, &s_DoorSeatModule_Data, sizeof(DoorSeatModule_t));
            break;
        case E_IAPP_Adas:
            memcpy(data, &s_AdasSetting_Data, sizeof(AdasSetting_t));
            break;
        case E_IAPP_SeatMirrorBind:
            memcpy(data, &s_SeatMirrorBind_Data, sizeof(SeatMirrorBind_t));
            break;
        case E_IAPP_new_energy:
            memcpy(data, &s_New_Energy_Data, sizeof(New_Energy_t));
            break;
        case E_IAPP_APP_RVCAVM_TYPE:
            memcpy(data, &s_RVCAVM_TYPE_Data, sizeof(RVCAVM_TYPE_t));
            break;
        case E_IAPP_HVAC_TYPE:
            memcpy(data, &s_HVAC_TYPE_Data, sizeof(HVAC_TYPE_t));
            break;
        case E_IAPP_APP_RVCAVM_TYPE:
            memcpy(data, &s_UserIdOperate_Data, sizeof(UserIdOperate_t));
            break;
        case E_IAPP_HUT_Settings:
            memcpy(data, &s_Hut_Settings_Data, sizeof(Hut_Settings_t));
            break;
        case E_IAPP_PASSENGER_SEAT:
            memcpy(data, &s_PassSeatBind_Data, sizeof(PassSeatBind_t));
            break;
        case E_IAPP_C03_C01_TYPE:
            memcpy(data, &s_C03_C01_TYPE_Data, sizeof(C03_C01_TYPE_t));
            break;
        case E_IAPP_EU_TYPE:
            memcpy(data, &s_EU_TYPE_Data, sizeof(EU_TYPE_t));
            break;
        case E_IAPP_APP_LOGIN_TYPE:
            memcpy(data, &s_APP_LOGIN_TYPE_Data, sizeof(APP_LOGIN_TYPE_t));
            break;
        case E_IAPP_APP_WIND:
            memcpy(data, &s_APP_Wind_Data, sizeof(APP_Wind_t));
            break;
        case E_IAPP_GetAll:
            memcpy(data, &s_GetAllData_Data, sizeof(GetAllData_t));
            break;
        case E_IAPP_GetSimple:
            memcpy(data, &s_GetOneData_Data, sizeof(GetOneData_t));
            break;
        default:
            break;
    }
}

void get_vipIAppSubItem_VimsDms_DistrctnDetnSet(uint8 *data) {
    *data = s_VimsDms_Data.DistrctnDetnSet;
}
void get_vipIAppSubItem_VimsDms_DrvDrowsnsDetnSet(uint8 *data) {
    *data = s_VimsDms_Data.DrvDrowsnsDetnSet;
}
void get_vipIAppSubItem_VimsDms_DrvDetectionSet(uint8 *data) {
    *data = s_VimsDms_Data.DrvDetectionSet;
}
void set_flag_90FDBUS_GetAll()
{
    /* 设置flag为2表示已经接收到通讯册获取所有数据的命令 */
    s_FdBus_90_getAlldataFlg = 2;
}
uint8 get_flag_90FDBUS_GetAll()
{
    /* 提供接收处理标志状态 */
    return s_FdBus_90_getAlldataFlg;
}
void update_flag_90FDBUS_GetAll()
{
    if(s_FdBus_90_getAlldataFlg)
        --s_FdBus_90_getAlldataFlg;
}
void clear_flag_90FDBUS_GetAll()
{
        s_FdBus_90_getAlldataFlg = 0;
}
#endif 